//
//  LiveView.swift
//  GFQM
//
//  Created by iSolar on 2017/4/21.
//  Copyright © 2017年 iSolar. All rights reserved.
//

import UIKit

class LiveView: UIScrollView ,UIScrollViewDelegate , CirCleViewDelegate {
    
    var circleView:CirCleView!
    let titleView:UIView = UIView()
    let titleLabel = UILabel()
    let numLabel = UILabel()
    let arrowV = UIImageView()
    
    let imageArray: [UIImage?] = [UIImage(named: "1.png"), UIImage(named: "2.png"), UIImage(named: "3.png")]
    var images = ["img1","img2","img3","img4","img5","img6"]
    var titleArr = ["个性化推荐你喜欢的歌曲","唯美古风 倾心沉醉","萌萌侵略者【快听】","【知性】----成熟女性最好的标志","【安静向】觉得烦躁时不妨听一下","如果这都不算爱 ？？？"]
    
    let KScreenWidth: CGFloat = UIScreen.main.bounds.width
    let KScreenHeight: CGFloat = UIScreen.main.bounds.height
    
    //MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentSize = CGSize(width:0,height:KScreenWidth*2+100)
        self.showsVerticalScrollIndicator = true
        self.showsHorizontalScrollIndicator = false
        self.isScrollEnabled = true // 可以上下滚动
        setupCircleView()
        setupTitleView()
        setupContentView()
    }
    
    func setupCircleView() {
        self.circleView = CirCleView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 160), imageArray: imageArray)
        circleView.delegate = self
        addSubview(self.circleView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- Delegate Methods
    //MARK: CirCleViewDelegate Methods
    func clickCurrentImage(_ currentIndxe: Int) {
        print(currentIndxe, terminator: "");
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 上下拉时滚动时的位移量（向上滚动时，位移是正数；向下拉动时，位移是负数）
        let offsetY = scrollView.contentOffset.y
        print("offsetY = \(offsetY)")
        
        // 最大位移量（注意scrollview的contentsize的高与当前视图控制器的屏幕高度的关系）
        let offsetYMax = scrollView.contentSize.height
        print("offsetYMax = \(offsetYMax)")
        let offsetYTmp = offsetYMax - self.frame.size.height
        print("offsetYTmp = \(offsetYTmp)")
        
        if 0 >= offsetY
        {
            // 向下拉
            if -100.0 >= offsetY {
                scrollView.contentInset = UIEdgeInsetsMake(80.0, 0.0, 0.0, 0.0)
            }
        } else {
            // 向上拉
            let offsetYUp = offsetY - offsetYTmp
            if 100.0 <= offsetYUp {
                scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 80.0, 0.0)
            }
        }
    }

}

extension LiveView{
    fileprivate func setupTitleView(){
        addSubview(titleView)
        titleView.frame = CGRect(x: 0, y: 170, width: KScreenWidth, height: 40)
        titleLabel.frame = CGRect(x: (KScreenWidth-80)/2, y: 10, width: 80, height: 20)
        titleView.addSubview(titleLabel)
        titleLabel.text = "为你推荐"
        titleLabel.textColor = UIColor.black
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.init(name: "Helvetica-Bold", size: 17)
        
        arrowV.frame = CGRect(x:KScreenWidth-30, y:13, width:15, height:15)
        titleView.addSubview(arrowV)
        arrowV.image = UIImage(named:"arrow")
        
        numLabel.frame = CGRect(x: KScreenWidth/2+50, y: 10, width: KScreenWidth/2 - 80, height: 20)
        titleView.addSubview(numLabel)
        numLabel.textAlignment = .right
        numLabel.textColor = UIColor.lightGray
        numLabel.text = "203人在直播"
        numLabel.font = UIFont.systemFont(ofSize: 12)
    }
    
    fileprivate func setupContentView(){
        //定义collectionView的布局类型，流布局
        let layout = UICollectionViewFlowLayout()
        //设置cell的大小
        layout.itemSize = CGSize(width: (KScreenWidth-30)/2, height: (KScreenWidth-30)/2+40)
        //        //滑动方向 默认方向是垂直
        //        layout.scrollDirection = .vertical
        //每个Item之间最小的间距
        layout.minimumInteritemSpacing = 0
        //每行之间最小的间距
        layout.minimumLineSpacing = 0
        //定义一个UICollectionView
        let collectionView = UICollectionView(frame: CGRect(x: 0,y: 210,width: KScreenWidth,height: ((KScreenWidth-30)/2+40)*3+15), collectionViewLayout: layout)
        //设置collectionView的背景颜色
        collectionView.backgroundColor = UIColor.white
        addSubview(collectionView)
        //设置collectionView的代理和数据源
        collectionView.isScrollEnabled = false
        collectionView.delegate = self
        collectionView.dataSource = self
        //CollectionViewCell的注册
        collectionView.register(UINib(nibName: "LiveCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCell")
    }
    
}

extension LiveView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    // #MARK: --UICollectionViewDataSource的代理方法
    /**
     - 该方法是可选方法，默认为1
     - returns: CollectionView中section的个数
     */
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    /**
     - returns: Section中Item的个数
     */
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    /**
     - returns: 绘制collectionView的cell
     */
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! LiveCollectionViewCell
        
        cell.imageView.image = UIImage(named: images[indexPath.item])
        cell.title.text = titleArr[indexPath.item]
        cell.title.font = UIFont.systemFont(ofSize: 14)
        cell.title.numberOfLines = 0
        
        return cell
    }
    
    // #MARK: --UICollectionViewDelegate的代理方法
    /**
     Description:当点击某个Item之后的回应
     */
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("(\(indexPath.section),\(indexPath.row))")
    }
    
    /**
     Description:可以定制不同的item
     
     - returns: item的大小
     */
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (KScreenWidth-30)/2, height: (KScreenWidth-30)/2+40)
    }
    
    //返回cell 上下左右的间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
        return UIEdgeInsetsMake(10, 10, 10, 10)
    }
}



